package com.keba.lamp.base.manager.user.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.keba.basic.base.manager.impl.SuperCacheManagerImpl;
import com.keba.basic.database.mybatis.conditions.Wraps;
import com.keba.basic.model.cache.CacheKeyBuilder;
import com.keba.basic.utils.ArgumentAssert;
import com.keba.basic.utils.CollHelper;
import com.keba.lamp.base.entity.user.BaseEmployee;
import com.keba.lamp.base.manager.user.BaseEmployeeManager;
import com.keba.lamp.base.mapper.ignore.BaseEmployeeIgnoreMapper;
import com.keba.lamp.base.mapper.user.BaseEmployeeMapper;
import com.keba.lamp.base.vo.query.user.BaseEmployeePageQuery;
import com.keba.lamp.base.vo.result.user.BaseEmployeeResultVO;
import com.keba.lamp.common.cache.base.user.EmployeeCacheKeyBuilder;
import com.keba.lamp.model.constant.EchoApi;
import com.keba.lamp.system.vo.result.tenant.DefUserTenantRelResultVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 通用业务实现类
 * 员工
 * </p>
 *
 * @author zuihou
 * @date 2021-10-18
 * @create [2021-10-18] [zuihou] [代码生成器生成]
 */
@Slf4j
@Service(EchoApi.BASE_EMPLOYEE_SERVICE_IMPL_CLASS)
@RequiredArgsConstructor
public class BaseEmployeeManagerImpl extends SuperCacheManagerImpl<BaseEmployeeMapper, BaseEmployee> implements BaseEmployeeManager {
    private final BaseEmployeeIgnoreMapper baseEmployeeIgnoreMapper;

    @Override
    protected CacheKeyBuilder cacheKeyBuilder() {
        return new EmployeeCacheKeyBuilder();
    }

    @Override
    public Map<Serializable, Object> findByIds(Set<Serializable> ids) {
        List<BaseEmployee> baseEmployees = list(Wraps.<BaseEmployee>lbQ().select(BaseEmployee::getUserId, BaseEmployee::getRealName).in(BaseEmployee::getUserId, ids));
        return CollHelper.uniqueIndex(baseEmployees, BaseEmployee::getUserId, BaseEmployee::getRealName);
    }

    @Override
    public IPage<BaseEmployeeResultVO> selectPageResultVO(IPage<BaseEmployee> page, Wrapper<BaseEmployee> wrapper, BaseEmployeePageQuery model) {
        return baseMapper.selectPageResultVO(page, wrapper, model);
    }

    @Override
    public List<DefUserTenantRelResultVO> listEmployeeByUserId(Long userId) {
        return baseEmployeeIgnoreMapper.listEmployeeByUserId(userId);
    }

    @Override
    public List<DefUserTenantRelResultVO> listEmployeeByUserIdAndTenantId(Long userId, Long tenantId) {
        return baseEmployeeIgnoreMapper.listEmployeeByUserIdAndTenantId(userId, tenantId);
    }

    @Override
    public BaseEmployee getEmployeeByTenantAndUser(Long tenantId, Long userId) {
        ArgumentAssert.notNull(tenantId, "租户id为空");
        ArgumentAssert.notNull(userId, "用户id为空");
        return baseEmployeeIgnoreMapper.selectOne(Wraps.<BaseEmployee>lbQ().eq(BaseEmployee::getTenantId, tenantId).eq(BaseEmployee::getUserId, userId));
    }

    @Override
    public long countIgnore(Wrapper<BaseEmployee> queryWrapper) {
        return baseEmployeeIgnoreMapper.selectCount(queryWrapper);
    }

    @Override
    public List<BaseEmployee> listIgnore(Wrapper<BaseEmployee> wrapper) {
        return baseEmployeeIgnoreMapper.selectList(wrapper);
    }

    @Override
    public boolean updateIgnore(Wrapper<BaseEmployee> wrapper) {
        return baseEmployeeIgnoreMapper.update(null, wrapper) > 0;
    }
}
